﻿@inject IAssistantService _assistantService
@inject ChatState _chatState
@implements IDisposable

<MudDrawerContainer Class="mud-height-full">
    <MudDrawer @bind-Open="IsOpen" Anchor="Anchor.Left" DisableOverlay="true" Elevation="8">
        @if (isEditing)
        {
            <DrawerHeader Title="@(SelectedAssistantId == Guid.Empty ? "Create a new Assistant" : "Edit the Assistant")" OnClose="CloseDrawer" />
            <AssistantForm AssistantId="SelectedAssistantId" OnAssistantSubmit="HandleAssistantSubmitted" />
        }
        else
        {
            <DrawerHeader Title="Select or Manage Assistants" OnClose="CloseDrawer" DisplayDivider="false"/>
            <MudList Clickable="true" Class="pt-0" @bind-SelectedItem="SelectedItem" @bind-SelectedValue="SelectedValue" Color="@(_IsDarkModeEnabled ? Color.Tertiary : Color.Dark)">
                <MudDivider />
                @foreach (var assistant in Assistants)
                {
                    <AssistantItem Assistant="@assistant"
                                   OnEdit="() => EditAssistant(assistant.Id)"
                                   OnDelete="HandleAssistantDeleted" />
                }
            </MudList>
            <MudSpacer/>
            <MudPaper class="d-flex flex-row justify-end gap-4 pa-4" Style="@BackgroundColorStyle">
                <MudButton OnClick="CreateNewAssistant" Variant="Variant.Filled" Color="Color.Primary">New Assistant</MudButton>
            </MudPaper>
        }
    </MudDrawer>
</MudDrawerContainer>

@code 
{
    [Parameter]
    public bool IsOpen { get; set; }

    [Parameter]
    public EventCallback<bool> IsOpenChanged { get; set; }

    [CascadingParameter]
    private bool _IsDarkModeEnabled { get; set; }

    private MudListItem? SelectedItem { get; set; }
    private object? SelectedValue { get; set; }
    private Guid SelectedAssistantId { get; set; }

    private string BackgroundColorStyle => $"background-color: {(_IsDarkModeEnabled ? "#1e1f22" : "#d6d3d1")};";
    private bool isEditing = false;

    public List<AssistantResponse> Assistants { get; set; } = new();

    protected override async Task OnInitializedAsync()
    {
        _chatState.SelectedAssistantChanged += StateHasChanged;

        await LoadAssistants();
        SelectDefaultAssistantItem();
    }

    private async Task LoadAssistants()
    {
        var response = await _assistantService.BrowseAssistants();
        var assistants = response.Value;

        Assistants = assistants.Items.ToList();
    }

    private void CloseDrawer()
    {
        IsOpen = false;
        isEditing = false;
        IsOpenChanged.InvokeAsync(IsOpen);
    }

    private void CreateNewAssistant()
    {
        SelectedAssistantId = Guid.Empty;
        isEditing = true;
    }

    private void EditAssistant(Guid id)
    {
        SelectedAssistantId = id;
        isEditing = true;
    }

    private async Task HandleAssistantSubmitted()
    {
        isEditing = false;
        await LoadAssistants();
    }

    private async Task HandleAssistantDeleted()
    {
        await HandleAssistantSubmitted();
        SelectDefaultAssistantItem();
    }

    private void SelectDefaultAssistantItem()
    {
        var defaultAssistantToSelect = Assistants.SingleOrDefault(assistant => assistant.IsDefault == true);

        if (defaultAssistantToSelect is not null)
        {
            SelectedItem = new MudListItem
            {
                Value = defaultAssistantToSelect.Id
            };

            SelectedValue = defaultAssistantToSelect.Id;

            _chatState.SetSelectedAssistant(defaultAssistantToSelect);
            StateHasChanged();
        }

    }

    public void Dispose()
    {
        _chatState.SelectedAssistantChanged -= StateHasChanged;
    }
}
